<?php
header('Content-Type: application/json; charset=utf-8');
defined('IN_PHPCMS') or exit('error! No permission.');
$action = safe_replace($_REQUEST['action']);
if (function_exists($action) && isset($action)) {
	$action();
}else{
	exit('error! No action.');
}

//首页
function index() {
	//$catcache = getcache('category_content_1','commons');
	//result = multi_array_sort($catcache,'listorder')
	$output = array();
	$output['ad']=getad(19,4);
	
	$member_db = pc_base::load_model('member_model');
	$result = $member_db->query("SELECT * FROM phpcms_category where (parentid='7' or parentid='8') AND catid!='8' ORDER BY listorder ASC");
	$result = $member_db->fetch_array($result);
	if($result){
		foreach($result as  $i => $r) {
			if($r['catid']){
				$output['nav'][$i]['id'] = $r['catid'];
				$output['nav'][$i]['type'] = $r['description'];
				$output['nav'][$i]['thumb'] = $r['image']; 
				$output['nav'][$i]['name'] = $r['catname']; 
			}
		}
	}else{
		$output['nav'] ="";
	}
	outget(JSON($output));
}

//新闻
function list_news() {
	$catid = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id.');
	$member_db = pc_base::load_model('member_model');
	$pagenum = 10;
	if($_REQUEST['page']){ $pagesize = intval($_REQUEST['page']); }else{$pagesize =1;}
	
	$output = array();
	$output['ad']=getad(20);

	$countrs = $member_db->query("SELECT count(*) FROM phpcms_news WHERE status='99' and catid=".$catid);   //取数量,判断是否有分页
	$myrow = mysql_fetch_array($countrs);
	$numrows=$myrow[0];
	$isnext = $numrows-$pagesize*$pagenum;
	if($isnext>0){$isnext=1;}else{$isnext=0;}
	$sqlpage = ($pagesize-1)*$pagenum;	
	
	$r = $member_db->query("SELECT * FROM phpcms_news where status='99' and catid=".$catid." ORDER BY listorder desc LIMIT ".$sqlpage.",".$pagenum);
	$r = $member_db->fetch_array($data);
	if($r){
		foreach ($r AS $i => $row){ 
			$output['list'][$i]['id'] = $row['id'];
			$output['list'][$i]['title'] = str_replace('"',"'",$row['title']);
			$output['list'][$i]['thumb'] = str_replace('"',"'",$row['thumb']);
			$output['list'][$i]['description'] = str_replace('"',"'",$row['description']);
			$output['list'][$i]['time'] = $row['inputtime'];
			$output['list'][$i]['url'] = $row['url'];
		}
	}else{
		$output['list'] = "";
	}
	$output['next'] = $isnext;
	$output['page'] = $pagesize;
	
	
	outget(JSON($output));
}

function list_hotel() {
	$member_db = pc_base::load_model('member_model');
	$catid = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id.');
	if($_REQUEST['city']){
		$city = intval($_REQUEST['city']);
	}else{
		$city =1;
	}
	
	$key = isset($_REQUEST['key']) ? safe_replace($_REQUEST['key']) : '';
	$orderby = isset($_REQUEST['orderby']) ? $_REQUEST['orderby'] : 'recommend';
 
	
	$output = array();
	$r = $member_db->query("SELECT setting FROM phpcms_model_field WHERE fieldid=142");   //地区
	$r  =mysql_fetch_assoc($r);
	$citylist = string2array($r['setting']);
	$options = explode("\n", $citylist['options']);
	if($options){
		foreach ($options as $i => $s){
			list($k,$v)=explode('|',$s);
			$output['city'][$i]['id']= trim($v);
			$output['city'][$i]['name']=  str_replace("\n","",$k);
		}
	}else{
		$output['city'] = "";
	}
	
	$output['ad']=getad(21);//广告 
	
	if($key){
		$searchwhere = " AND  b.name LIKE '%".$key."%' ";
	}
	
	$pagenum = 10;
	if($_REQUEST['page']){ $pagesize = intval($_REQUEST['page']); }else{$pagesize =1;}
	$countrs = $member_db->query("SELECT count(*) FROM v9_member a,v9_member_shan b,v9_goods c WHERE a.userid=b.userid AND a.userid=c.userid AND b.type=c.catid  AND c.catid=".$catid.$searchwhere." AND c.inputtime IN (SELECT MAX(inputtime) FROM v9_goods GROUP BY userid)");   //取数量,判断是否有分页
	$myrow = mysql_fetch_array($countrs);
	$numrows=$myrow[0];
	$isnext = $numrows-$pagesize*$pagenum;
	if($isnext>0){$isnext=1;}else{$isnext=0;}
	$sqlpage = ($pagesize-1)*$pagenum;
	
	
	if($orderby=='stock'){ 
		$getsql="SELECT *,@nums:=(SELECT SUM(stock) FROM v9_goods WHERE userid=c.userid) FROM v9_member a,v9_member_shan b,v9_goods c WHERE a.userid=b.userid AND a.userid=c.userid AND b.type=c.catid  AND c.catid=".$catid." AND c.city=".$city.$searchwhere." AND c.inputtime IN (SELECT MAX(inputtime) FROM v9_goods GROUP BY userid) ORDER BY @nums:=(SELECT SUM(stock) FROM v9_goods WHERE userid=c.userid) DESC limit ".$sqlpage.",".$pagenum;
	}elseif($orderby =='price'){
		$getsql="SELECT * FROM v9_member a,v9_member_shan b,v9_goods c WHERE a.userid=b.userid AND a.userid=c.userid AND b.type=c.catid AND c.catid=".$catid." AND c.city=".$city.$searchwhere."  AND c.price=(SELECT price FROM v9_goods WHERE userid=a.userid GROUP BY userid ORDER BY price ASC LIMIT 1)  GROUP BY a.userid ORDER BY c.price ASC limit ".$sqlpage.",".$pagenum;
	}else{
		$getsql="SELECT a.userid,b.name,b.image,b.intro,c.price,b.zhekou,b.stat,b.shoptype FROM v9_member a,v9_member_shan b,v9_goods c WHERE a.userid=b.userid AND a.userid=c.userid AND b.type=c.catid  AND c.catid=".$catid." AND c.city=".$city.$searchwhere."  AND c.price=(SELECT price FROM v9_goods WHERE userid=a.userid GROUP BY userid ORDER BY price ASC LIMIT 1)  GROUP BY a.userid ORDER BY b.listorder DESC limit ".$sqlpage.",".$pagenum;
	}
	
	$r = $member_db->query($getsql);
	$r = $member_db->fetch_array($data);
	if($r){
		foreach ($r AS $i => $row){ 
			$thumb = string2array($row['pics']);
			$output['list'][$i]['id'] = $row['userid'];
			$output['list'][$i]['title'] = str_replace('"',"'",$row['name']);
			$output['list'][$i]['stat'] = $row['stat'];
			$output['list'][$i]['goodtype'] = explode(',',substr(substr($row['shoptype'],1),0,'-1'));
			$output['list'][$i]['thumb'] = $row['image'];
			$output['list'][$i]['price'] = $row['price'];
			$output['list'][$i]['price_vip'] = price_format($row['price']*($row['zhekou']*0.01));
		}
	}else{
		$output['list'] = "";
	}
	$output['next'] = $isnext;
	$output['page'] = $pagesize;
	
	
	outget(JSON($output));
}


function list_goods() {
	$member_db = pc_base::load_model('member_model');
	$catid = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id.');
	if($_REQUEST['city']){
		$city = intval($_REQUEST['city']);
	}else{
		$city =1;
	}
	$key = isset($_REQUEST['key']) ? safe_replace($_REQUEST['key']) : '';
	$orderby = isset($_REQUEST['orderby']) ? $_REQUEST['orderby'] : 'recommend';
 

 
	$r = $member_db->query("SELECT setting FROM phpcms_model_field WHERE fieldid=142");   //地区
	$r  =mysql_fetch_assoc($r);
	$citylist = string2array($r['setting']);
	$options = explode("\n", $citylist['options']);
	if($options){
		foreach ($options as $i => $s){
			list($k,$v)=explode('|',$s);
			$output['city'][$i]['id']=trim($v);
			$output['city'][$i]['name']=  str_replace("\n","",$k);
		}
	}else{
		$output['city'] = "";
	}
	
	$output['ad']=getad(21);//广告 
	
	if($key){
		$searchwhere = " AND c.title LIKE '%".$key."%' ";
	}
	
	if($orderby=='hot'){
		$countsql = "SELECT count(*) FROM v9_member a,v9_member_shan b,v9_goods c WHERE a.userid=b.userid AND a.userid=c.userid AND c.goodtype='goods' AND c.catid=".$catid." AND c.city=".$city.$searchwhere;
		$datasql = "SELECT * FROM v9_member a,v9_member_shan b,v9_goods c WHERE a.userid=b.userid AND a.userid=c.userid AND c.goodtype='goods' AND c.catid=".$catid.$searchwhere." AND c.city=".$city." order by c.hot DESC";
	}elseif($orderby=='new'){
		$countsql = "SELECT count(*) FROM v9_member a,v9_member_shan b,v9_goods c WHERE a.userid=b.userid AND a.userid=c.userid AND c.goodtype='goods' AND c.catid=".$catid." AND c.city=".$city.$searchwhere;
		$datasql = "SELECT * FROM v9_member a,v9_member_shan b,v9_goods c WHERE a.userid=b.userid AND a.userid=c.userid AND c.goodtype='goods' AND c.catid=".$catid.$searchwhere." AND c.city=".$city." order by c.inputtime DESC";
	}else{
		$countsql = "SELECT count(*) FROM v9_member a,v9_member_shan b,v9_goods c WHERE a.userid=b.userid AND a.userid=c.userid AND c.goodtype='goods' AND c.catid=".$catid." AND c.city=".$city.$searchwhere;
		$datasql = "SELECT * FROM v9_member a,v9_member_shan b,v9_goods c WHERE a.userid=b.userid AND a.userid=c.userid AND c.goodtype='goods' AND c.catid=".$catid.$searchwhere." AND c.city=".$city." order by c.listorder DESC";
	}
	
	
	$pagenum = 10;
	if($_REQUEST['page']){ $pagesize = intval($_REQUEST['page']); }else{$pagesize =1;}
	$countrs = $member_db->query($countsql);   //取数量,判断是否有分页
	$myrow = mysql_fetch_array($countrs);
	$numrows=$myrow[0];
	$isnext = $numrows-$pagesize*$pagenum;
	if($isnext>0){$isnext=1;}else{$isnext=0;}
	$sqlpage = ($pagesize-1)*$pagenum;	
	
	
	$r = $member_db->query($datasql." LIMIT ".$sqlpage.",".$pagenum);
	$r = $member_db->fetch_array($data);
	if($r){
		foreach ($r AS $i => $row){ 
			$output['list'][$i]['id'] = $row['userid'];
			$output['list'][$i]['goodid'] = $row['id'];
			$output['list'][$i]['title'] = str_replace('"',"'",$row['title']);
			$output['list'][$i]['shop'] = $row['name'];
			$output['list'][$i]['thumb'] = $row['thumb'];
			$output['list'][$i]['price'] = $row['price'];
			$output['list'][$i]['price_vip'] = price_format($row['price']*($row['zhekou']*0.01));
			$output['list'][$i]['sales'] = $row['hot'];
		}
	}else{
		$output['list'] = "";
	}
	$output['next'] = $isnext;
	$output['page'] = $pagesize;
	
	
	outget(JSON($output));
}


function content(){
	$member_db = pc_base::load_model('member_model');
	
	$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');
	
	$q=$member_db->query("SELECT * FROM phpcms_member_shan WHERE userid=".$id);
	$r=mysql_fetch_assoc($q);
	if($r){ 
		$reviewcount=$member_db->query("SELECT count(*) FROM v9_review WHERE shop_id=".$id);
		$reviewcount=mysql_fetch_array($reviewcount);
		
		$reviewlist = $member_db->query("SELECT * FROM v9_member_detail a, v9_review b WHERE a.userid=b.user_id and b.shop_id=".$id." ORDER BY b.good DESC LIMIT 3");
		$reviewlist = $member_db->fetch_array($reviewlist);
		
		foreach ($reviewlist as $k => $row){
			$review[$k]['id'] =  $row['id'];
			$review[$k]['thumb'] =  $row['image'];
			$review[$k]['time'] =  $row['time'];
			$review[$k]['name'] =  $row['name'];
			$review[$k]['content'] =  $row['content'];
		}
			
		$shoplist = $member_db->query("SELECT * FROM phpcms_member_shan WHERE type=".$r[type]." AND userid!=".$r[userid]."  ORDER BY rand() LIMIT 3");
		$shoplist = $member_db->fetch_array($shoplist);
		
		foreach ($shoplist as $k => $row){
			$recommand[$k]['id'] =  $row['userid'];
			$recommand[$k]['name'] =  $row['name'];
			$recommand[$k]['thumb'] =  $row['image'];
		}
	
		$output['name'] = $r['name'];
		$output['tel'] = $r['tel'];
		$output['address'] = $r['address'];
		$output['intro'] = $r['intro'];
		$output['position'] = $r['position'];
		$output['shop_open'] = $r['time_open'];
		$output['shop_close'] = $r['time_close'];
		$output['photos'] = string2array($r['pics']);
		$output['review_count'] = $reviewcount[0];
		$output['review_more'] = 'http://www.baidu.com/#'.$reviewcount[0];
		$output['review'] = $review;
		$output['recommand'] = $recommand;//广告推荐
	}else{
		$output['status'] = '0';
		$output['info'] = '19';
	}
	
	outget(JSON($output));
}

function content_attr_day(){
	$member_db = pc_base::load_model('member_model');
	$shang_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id.');
	$dayshop = isset($_REQUEST['time']) ? $_REQUEST['time'] : date('Y-m-d',SYS_TIME);
	$daynum = isset($_REQUEST['num']) ? intval($_REQUEST['num']) : 1;

	$sj=$member_db->query("SELECT * FROM phpcms_member_shan WHERE userid=".$shang_id);
	$sj=mysql_fetch_assoc($sj);
	if(!$sj){ exit("对不起,找不到您需求的商家!");}
	
	$r = $member_db->query("SELECT * FROM v9_goods a,v9_goods_data b WHERE a.id=b.id AND a.catid=".$sj[type]." AND a.goodtype='day'  AND a.userid=".$sj[userid]." ORDER BY a.listorder DESC");
	$r = $member_db->fetch_array($data);
	
	foreach ($r AS $k => $row){ 
	
		$theday = date('Y-m-d',strtotime($dayshop));
		$aa = array();
		$times = array();
		$order_db =  pc_base::load_model('order_model');
		$sql = "SELECT * FROM phpcms_order WHERE timeoutshop > ".$theday." AND timeshop > ".$theday." AND frist_good=".$row[id];
		$ordercount = $order_db->query($sql); 
		$ordercount = $member_db->fetch_array($ordercount);
		foreach ($ordercount AS $i => $rows){ 
			$goods = string2array($rows['goods']);
			$aa[$i]['num'] = $goods[$row[id]]['num'];
			$aa[$i]['dayshop'] = strtotime(date('Y-m-d',$rows['timeshop']));
		}
		//print_r($aa);
		foreach($aa as $vv){
			for($i=0;$i<$vv['num'];$i++){
				$pretime=$i*24*60*60; 
				$times[$vv['dayshop']+$pretime] = $times[$vv['dayshop']+$pretime]+1;
			}
		}
		//print_R($times);
		for($i=0;$i<$daynum;$i++){
			//$dayshopchuo = strtotime($dayshop);
			$dayshopchuo = strtotime(date('Y-m-d',strtotime($dayshop)));
			$pretime=$i*24*60*60; 
			$dayshopchuo = $dayshopchuo+$pretime;
			//echo $dayshopchuo;
			if(($row[stock]-$times[$dayshopchuo])<1){
				$btnbuyclasss= '0';
				 break;
			}else{
				$btnbuyclasss= '1';
			}
		}
	
		$output[$k]['id'] = $row['id'];
		$output[$k]['title'] = str_replace('"',"'",$row['title']);
		$output[$k]['price'] = $row['price'];
		$output[$k]['price_vip'] = price_format($row['price']*($sj[zhekou]*0.01));
		$output[$k]['discount'] = $sj[zhekou];
		$output[$k]['description'] = str_replace('"',"'",$row['description']);
		$output[$k]['stock'] = $btnbuyclasss;
	}
	outget(JSON($output));
}

function content_attr_time(){
	$member_db = pc_base::load_model('member_model');
	$shang_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id.');
	$timeshop = isset($_REQUEST['time']) ? $_REQUEST['time'] : date('H',SYS_TIME);
	$timenum = isset($_REQUEST['num']) ? intval($_REQUEST['num']) : 1;

	$sj=$member_db->query("SELECT * FROM phpcms_member_shan WHERE userid=".$shang_id);
	$sj=mysql_fetch_assoc($sj);
	if(!$sj){ exit("对不起,找不到您需求的商家!");}
	
	$r = $member_db->query("SELECT * FROM v9_goods a,v9_goods_data b WHERE a.id=b.id AND a.catid=".$sj[type]." AND a.goodtype='time' AND a.userid=".$sj[userid]." ORDER BY a.listorder DESC");
	$r = $member_db->fetch_array($data);
	
	foreach ($r AS $k => $row){ 
	
		$aa = array();
		$times = array();
		$stocktime = "";
		$order_db =  pc_base::load_model('order_model');
		$sql = "SELECT * FROM v9_order WHERE (DATE_FORMAT(FROM_UNIXTIME(timeoutshop),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') OR DATE_FORMAT(FROM_UNIXTIME(timeshop),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')) AND frist_good=".$row[id];
		$ordercount = $order_db->query($sql); 
		$ordercount = $member_db->fetch_array($ordercount);
		foreach ($ordercount AS $i => $rows){ 
			$goods = string2array($rows['goods']);
			$aa[$i]['num'] = $goods[$row[id]]['num'];
			$aa[$i]['timeshop'] = $rows['timeshop'];
		}
		//print_r($aa);
		foreach($aa as $vv){
			//for($i=0;$i<=$vv['num'];$i++){ 后面一小时也可以
			for($i=0;$i<$vv['num'];$i++){
				//$times[$i+$vv['timeshop']] = $times[$i+$vv['timeshop']]+1;
				$pretime=$i*60*60; 
				$times[$vv['timeshop']+$pretime] = $times[$vv['timeshop']+$pretime]+1;
			}
		}
		//print_R($times);
		for($i=0;$i<$timenum;$i++){
			if($i==0){ $timeshop = $timeshop; }else{	$timeshop = $timeshop+1;}
			$thistime=  strtotime(date('Y-m-d',SYS_TIME).' '.$timeshop.":00:00");
			//echo $timeshop;
			if(($row[stock]-$times[$thistime])<1){
				$btnbuyclasss= '0';
				 break;
			}else{
				$btnbuyclasss= '1';
			}
		 }
	
		$output[$k]['id'] = $row['id'];
		$output[$k]['title'] = str_replace('"',"'",$row['title']);
		$output[$k]['price'] = $row['price'];
		$output[$k]['price_vip'] = price_format($row['price']*($sj[zhekou]*0.01));
		$output[$k]['discount'] = $sj[zhekou];
		$output[$k]['description'] = str_replace('"',"'",$row['description']);
		$output[$k]['stock'] = $btnbuyclasss;
		$pricetime = array();
		for($j=0;$j<24;$j++){
			$pricetime[$j]=$row['time_'.$j];
		}
		$output[$k]['price_time'] = $pricetime;
	
		
		$thistime=  strtotime(date('Y-m-d ',SYS_TIME));
		$j = 0;
		for($i=$sj[time_open];$i<=$sj[time_close];$i++){
			$nextime=$i*60*60; 
			$dayshopchuo = $thistime + $nextime;
			if(($row[stock]-$times[$dayshopchuo])<1){
				$stocktime[$j] = $i;
				$j++;
			}
		}
		$output[$k]['stock_time'] = $stocktime;
		
	}
	outget(JSON($output));
}

function content_attr_goods(){
	$member_db = pc_base::load_model('member_model');
	$shang_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id.');
	$goodid = isset($_REQUEST['goodid']) ? intval($_REQUEST['goodid']) : '';
	$city = isset($_REQUEST['city']) ? intval($_REQUEST['city']) : 1;
	$key = isset($_REQUEST['key']) ? safe_replace($_REQUEST['key']) : '';
	$orderby = isset($_REQUEST['orderby']) ? $_REQUEST['orderby'] : 'recommend';
	
	$sj=$member_db->query("SELECT * FROM phpcms_member_shan WHERE userid=".$shang_id);
	$sj=mysql_fetch_assoc($sj);
	if(!$sj){ 
		exit("对不起,找不到您需求的商家!");
	}
	if($goodid){
		$goodidwhere = " field(id,".$goodid.") DESC, ";
	}
	if($key){
		$goodidwhere = '';
		$searchwhere = " AND title LIKE '%".$key."%' ";
	}
	
	if($orderby=='price'){
		$countsql = "SELECT count(*) FROM phpcms_goods where goodtype='goods' AND userid=".$sj['userid'].$searchwhere;
		$datasql = "SELECT * FROM phpcms_goods where goodtype='goods' AND userid=".$sj['userid'].$searchwhere."  ORDER BY".$goodidwhere." shoporder DESC";
	}elseif($orderby=='hot'){
		$countsql = "SELECT count(*) FROM phpcms_goods where goodtype='goods' AND userid=".$sj['userid'].$searchwhere;
		$datasql = "SELECT * FROM phpcms_goods where goodtype='goods' AND userid=".$sj['userid'].$searchwhere." ORDER BY".$goodidwhere." shoporder DESC";
	}else{
		$countsql = "SELECT count(*) FROM phpcms_goods where goodtype='goods' AND userid=".$sj['userid'].$searchwhere;
		$datasql = "SELECT * FROM phpcms_goods where goodtype='goods' AND userid=".$sj['userid'].$searchwhere." ORDER BY".$goodidwhere." shoporder DESC";
	}
	

	
	$pagenum = 10;
	if($_REQUEST['page']){ $pagesize = intval($_REQUEST['page']); }else{$pagesize =1;}
	$countrs = $member_db->query($countsql);   //取数量,判断是否有分页
	$myrow = mysql_fetch_array($countrs);
	$numrows=$myrow[0];
	$isnext = $numrows-$pagesize*$pagenum;
	if($isnext>0){$isnext=1;}else{$isnext=0;}
	$sqlpage = ($pagesize-1)*$pagenum;	
	
	
	$r = $member_db->query($datasql." LIMIT ".$sqlpage.",".$pagenum);
	$r = $member_db->fetch_array($data);
	foreach ($r AS $i => $row){ 
		$output['list'][$i]['id'] = $row['id'];
		$output['list'][$i]['title'] = str_replace('"',"'",$row['title']);
		$output['list'][$i]['thumb'] = $row['thumb'];
		$output['list'][$i]['price'] =$row['price'];
		$output['list'][$i]['price_vip'] = price_format($row['price']*($sj[zhekou]*0.01));
		$output['list'][$i]['sales'] = $row['hot'];
		$output['list'][$i]['stock'] = $row['stock'];
	}
	$output['next'] = $isnext;
	$output['page'] = $pagesize;
	
	outget(JSON($output));
}


function fav_add() { 
		$member_db = pc_base::load_model('member_model');
		$memberinfo = islogin();
		
		$favshop_db = pc_base::load_model('favshop_model');
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');
		
		$this_rv = $favshop_db->get_one(array('shop_id'=>$id,'user_id'=>$memberinfo['userid']));
		if($this_rv){
			$output['status']='0';
			$output['info']='18';
		}else{
			$favshop_db->insert(array('shop_id'=>$id,'user_id'=>$memberinfo['userid']));
			$output['status']='1';
		}
		outget(JSON($output));
}

function fav_list() { 
		$member_db = pc_base::load_model('member_model');
		$memberinfo = islogin();
		
		$favshop_db = pc_base::load_model('favshop_model');
		$pagenum = 20;
		if($_REQUEST['page']){ $pagesize = intval($_REQUEST['page']); }else{$pagesize =1;}
		$countrs = $favshop_db->query("SELECT count(*) FROM phpcms_favshop a,v9_member_shan b where a.shop_id=b.userid and a.user_id=".$memberinfo['userid']);   //取数量,判断是否有分页
		$myrow = mysql_fetch_array($countrs);
		$numrows=$myrow[0];
		$isnext = $numrows-$pagesize*$pagenum;
		if($isnext>0){$isnext=1;}else{$isnext=0;}
		$sqlpage = ($pagesize-1)*$pagenum;	
		
		$r = $member_db->query("SELECT * FROM phpcms_favshop a,v9_member_shan b where a.shop_id=b.userid and a.user_id=".$memberinfo['userid']." order by a.id DESC LIMIT ".$sqlpage.",".$pagenum);
		$r = $member_db->fetch_array($data);
		$catcache = getcache('category_content_1','commons');
		foreach ($r AS $i => $row){ 
			$output['list'][$i]['id'] = $row['userid'];
			$output['list'][$i]['title'] =  $row['name'];
			
			$output['list'][$i]['type'] =  $catcache[$row['type']]['description'];
			$output['list'][$i]['thumb'] = $row['image'];
		}
		$output['next'] = $isnext;
		$output['page'] = $pagesize;
		outget(JSON($output));
}

function fav_del() { 
		$member_db = pc_base::load_model('member_model');
		$memberinfo = islogin();
		
		$favshop_db = pc_base::load_model('favshop_model');
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');
		
		$this_rv = $favshop_db->delete(array('shop_id'=>$id,'user_id'=>$memberinfo['userid']));
		if($this_rv){
			$output['status']='1';
		}else{
			$output['status']='0';
		}
		outget(JSON($output));
}


function review_good() { 
		$review_db = pc_base::load_model('review_model');
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');
		
		$this_rv = $review_db->get_one(array('id'=>$id));
		if($this_rv){
			$review_db->update(array('good'=>$this_rv['good']+1),array('id'=>$id));
			$output['status']='1';
		}else{
			$output['status']='0';
		}
		outget(JSON($output));
}

function order_list() { 
		$member_db = pc_base::load_model('member_model');
		$memberinfo = islogin();
		
		$order_db = pc_base::load_model('order_model');
		$pagenum = 10;
		if($_REQUEST['page']){ $pagesize = intval($_REQUEST['page']); }else{$pagesize =1;}
		$countrs = $order_db->query("SELECT count(*) FROM v9_order where user_id=".$memberinfo['userid']);   //取数量,判断是否有分页
		$myrow = mysql_fetch_array($countrs);
		$numrows=$myrow[0];
		$isnext = $numrows-$pagesize*$pagenum;
		if($isnext>0){$isnext=1;}else{$isnext=0;}
		$sqlpage = ($pagesize-1)*$pagenum;	
		
		$r = $member_db->query("SELECT * FROM v9_order where user_id=".$memberinfo['userid']." order by order_id DESC LIMIT ".$sqlpage.",".$pagenum);
		$r = $member_db->fetch_array($r);
		if($r){
		$member_db->set_model(13);
			foreach ($r AS $i => $row){ 
				$shopinfo = $member_db->get_one(array('userid'=>$row['shop_id'])); 
				
				$k=0;
				foreach (string2array($row['goods']) as $good){
					$goods[$k]['id'] =  $good['id'];
					$goods[$k]['title'] =  $good['title'];
					$goods[$k]['goodtype'] =  $good['goodtype'];
					$goods[$k]['thumb'] =  $good['thumb'];
					$goods[$k]['num'] =  $good['num'];
					$goods[$k]['price'] =  $good['price'];
					$k++;
				
				}
				$output['list'][$i]['id'] = $row['order_id'];
				$output['list'][$i]['title'] =  $shopinfo['name'];
				$output['list'][$i]['tel'] =  $shopinfo['tel'];
				$output['list'][$i]['address'] = $shopinfo['address'];
				$output['list'][$i]['goods'] = $goods;
				$output['list'][$i]['status'] = $row['order_status'];
				$output['list'][$i]['name'] = $row['name'];
				$output['list'][$i]['mobile'] = $row['mobile'];
				$output['list'][$i]['time'] = $row['timeshop'];
				$output['list'][$i]['other'] = $row['other'];
				$output['list'][$i]['price'] = $row['price'];
				$output['list'][$i]['description'] = $row['order_des'];
				$output['list'][$i]['ship'] = $row['ship'];
				$output['list'][$i]['ship_address'] = $row['address'];
			}
		}else{
			$output['list'] ="";
		}
		$output['next'] = $isnext;
		$output['page'] = $pagesize;
		outget(JSON($output));
}


function order_review() { 
		$review_db = pc_base::load_model('review_model');
		$memberinfo = islogin();
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');
		
		$order_db = pc_base::load_model('order_model');
		$orderinfo = $order_db->get_one(array('order_id'=>$id,'user_id'=>$memberinfo['userid']));
		if($orderinfo) {
		
			$viewdata['user_id']  = $memberinfo['userid'];
			$viewdata['shop_id']  = $orderinfo['shop_id'];
			$viewdata['order_id']  = $id;
			$viewdata['time']  = SYS_TIME;
			$viewdata['content']  = isset($_REQUEST['content']) ? $_REQUEST['content'] : exit('error! No content .');
				
			$is_review = $review_db->get_one(array('order_id'=>$id));
			if($is_review){
				$output['status']='0';
				$output['info']='18';
			}else{
				$review_db->insert($viewdata);
				$output['status']='1';
			}
		}else{
			$output['status']='0';
			$output['info']='19';
		}
		outget(JSON($output));
}

function order_cancel() { 
		$review_db = pc_base::load_model('review_model');
		$memberinfo = islogin();
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');
		
		$order_db = pc_base::load_model('order_model');
		$is_cancel = $order_db->query('SELECT * FROM v9_order WHERE order_id='.$id.' AND user_id='.$memberinfo['userid'].' AND order_status IN (0,2)');
		$is_cancel = mysql_fetch_array($is_cancel);
		if($is_cancel){
			$order_db->query('UPDATE v9_order SET order_status="-1" WHERE order_id='.$id);
			$output['status']='1';
		}else{
			$output['status']='0';
			$output['info']='19';
		}
		outget(JSON($output));
}

function order_add() { 
		$order_db = pc_base::load_model('order_model');
		$member_db = pc_base::load_model('member_model');
		$memberinfo = islogin();
		
		$shopid  = isset($_REQUEST['shopid']) ? intval($_REQUEST['shopid']) : exit('error! No shopid .');
		$shopdata = $member_db->query("SELECT * FROM phpcms_member_shan  WHERE userid=".$shopid); 
		$shopdata  =mysql_fetch_assoc($shopdata);
		$totle = 0;
			
		$goods  = isset($_REQUEST['goods']) ? $_REQUEST['goods'] : exit('error! No goods .');
		$goodsdata = json_decode(stripslashes($goods),true);
		$first_id = $goodsdata[0]['id'];
		$first_num = $goodsdata[0]['num'];
		$con_db = pc_base::load_model('content_model');
		$con_db->set_model(14);
		
		foreach($goodsdata as  $i => $good){
				//$r =$con_db->get_one(array('id'=>$good['id']));
				$r = $con_db->query("SELECT * FROM phpcms_goods a,phpcms_goods_data b WHERE a.id=b.id AND a.id=".$good['id']); 
				$r  =mysql_fetch_assoc($r);
				if($r){
					$goodtype = $r['goodtype'];
					if(($r['stock']>=$good['num'] || $goodtype=='day' || $goodtype=='time') && $good['num']!==0){
						$gooddata[$r['id']]['id'] = $r['id'];
						$gooddata[$r['id']]['title'] = $r['title'];
						$gooddata[$r['id']]['goodtype'] = $r['goodtype'];
						$gooddata[$r['id']]['thumb'] = $r['thumb'];
						$gooddata[$r['id']]['num'] = $good['num'];
						$gooddata[$r['id']]['stock'] = $r['stock']-$good['num'];
						if($memberinfo['vip']==1){
							$price = price_format($r['price']*($shopdata['zhekou']*0.01));
						}else{
							$price = $r['price'];
						}
						$gooddata[$r['id']]['price'] = $price;
						$totle = $totle+$price*$good['num'];
					}else{
						$output['status']='0';
						$output['info']='21';
						outget(JSON($output));
					}
				}else{
					$output['status']='0';
					$output['info']='19';
					outget(JSON($output));
				}
			}
			
			$orderdata['name']  = isset($_REQUEST['name']) ? $_REQUEST['name'] : exit('error! No name .');
			$orderdata['mobile']  = isset($_REQUEST['mobile']) ? $_REQUEST['mobile'] :  exit('error! No mobile.');
			$orderdata['other']  = isset($_REQUEST['other']) ? $_REQUEST['other'] : '';
			$orderdata['ship']  = isset($_REQUEST['ship']) ? $_REQUEST['ship'] : '';
			$orderdata['address']  = isset($_REQUEST['address']) ? $_REQUEST['address'] : '';
			
			if($_REQUEST['timeshop']){  //验证时间
				if(validateDate($_REQUEST['timeshop'])==1){
					$orderdata['timeshop']  = isset($_REQUEST['timeshop']) ? $_REQUEST['timeshop'] :  exit('error! No timeshop.');
					$orderdata['timeoutshop']  =  $orderdata['timeshop']+($first_num*3600);
				}else{
					$output['status']='0';
					$output['info']='22';
					outget(JSON($output));
				}
			}
			if($orderdata['name']==''||$orderdata['mobile']==''||$orderdata['timeshop']==''){
				$output['status']='0';
				$output['info']='20';
				
				outget(JSON($output));
			}
			
			//小时房价格处理
			if($goodtype=='time'){
				$thetime = date('H',$orderdata['timeshop']); 
				
				if($shopdata['time_close']==24){}else{
					if(($thetime+$first_num)>($shopdata['time_close'])){ 
						$output['status']='0';
						$output['info']='20';
						outget(JSON($output));
					};
				}
				
				for($i=0;$i<$first_num;$i++){
						$retime = intval($thetime+$i);
						if($retime>23){
							$retime = $retime-24;
						}
					if($memberinfo['vip']==1){
						$r['price'] = price_format($r['price']*($shopdata['zhekou']*0.01));
					}
					$timegooddes = $timegooddes.$retime."点：".($r['price']+$r['time_'.$retime])."元;         ";
					$totala = $totala+($r['price']+$r['time_'.$retime]);
				}
				$orderdata['order_des']  = $timegooddes;
				$totle = $totala;
			}
			
			$orderdata['user_id']  = $memberinfo['userid'];
			$orderdata['shop_id']  = $shopid;
			$orderdata['price']  = price_format($totle);
			$orderdata['order_time']  = SYS_TIME;
			$orderdata['order_status']  = 0;
			$orderdata['frist_good']  =$first_id;
			$orderdata['goods']=array2string($gooddata);

			
			$order_id =$order_db->insert($orderdata,1);
			if($order_id){
				//商品库存处理
				if($goodtype=='goods'){
					foreach($gooddata as $val){
						$con_db->update(array('stock'=>$val['stock'],'hot'=>+$val['num']),array('id'=>$val['id']));
					} 
				}
				$output['status']='1';
				$output['info']='';
				$output['id']=$order_id;
				$output['total']=price_format($totle);
				
			}else{
				$output['status']='0';
				$output['info']='19';
			}
			
	outget(JSON($output));
}

function invite_add(){
	$invite_db = pc_base::load_model('invite_model');
	$order_db = pc_base::load_model('order_model');
	$member_db = pc_base::load_model('member_model');
	$memberinfo = islogin();
	
	$orderid  = isset($_REQUEST['orderid']) ? intval($_REQUEST['orderid']) : exit('error! No orderid .');
	$num  = isset($_REQUEST['num']) ? intval($_REQUEST['num']) : exit('error! No num .');
	$users   = isset($_REQUEST['users']) ? $_REQUEST['users'] : exit('error! No users .');
	$message   = isset($_REQUEST['message']) ? safe_replace($_REQUEST['message']) : exit('error! No message .');
	
	$is_invited = $invite_db->get_one(array('orderid'=>$orderid));
	if($is_invited){
		$output['status']='0';
		$output['info']='18';
		outget(JSON($output));
	}
	
	$orderinfo = $order_db->get_one(array('order_id'=>$orderid));
	if($orderinfo){
		if($orderinfo['timeshop']<SYS_TIME){
			$output['status']='0';
			$output['info']='22';
			outget(JSON($output));
		}
	}else{
		$output['status']='0';
		$output['info']='19';
		outget(JSON($output));
	}
	
	$userlist = explode(",", $users);
	if($userlist){
		foreach ($userlist as $i => $s){
			$userdata = $member_db->query("SELECT * FROM v9_member_detail WHERE userid=".$s); 
			$userdata  =mysql_fetch_assoc($userdata);
			if($userdata){
				$userarr[$i]['userid'] = $s;
				$userarr[$i]['name'] = $userdata['name'];
				$userarr[$i]['image'] = $userdata['image'];
				$userarr[$i]['status'] = 0;
			}else{
				$output['status']='0';
				$output['info']='16';
				outget(JSON($output));
			}
		}
	}else{
		$output['status']='0';
		$output['info']='16';
		outget(JSON($output));
	}
	$invitedata['orderid']  = $orderid;
	$invitedata['num']  = $num;
	$invitedata['message']  = $message;
	$invitedata['users']  = array2string($userarr);
	$invitedata['fctime']  = SYS_TIME;
	$invitedata['fcuser']  = $memberinfo['userid'];
	$invite_id =$invite_db->insert($invitedata,1);
	if($invite_id){
		$output['status']='1';
		$output['info']='';
		$output['id']=$invite_id;
	}else{
		$output['status']='0';
		$output['info']='16';
	}
	outget(JSON($output));
}

function invite_list(){
	$invite_db = pc_base::load_model('invite_model');
	$order_db = pc_base::load_model('order_model');
	$member_db = pc_base::load_model('member_model');
	$memberinfo = islogin();
	
	$pagenum = 10;
	if($_REQUEST['page']){ $pagesize = intval($_REQUEST['page']); }else{$pagesize =1;}
	$countrs = $invite_db->query("SELECT count(*) FROM v9_invite where fcuser=".$memberinfo['userid']." OR users LIKE '%".$memberinfo['userid']."%'");   //取数量,判断是否有分页
	$myrow = mysql_fetch_array($countrs);
	$numrows=$myrow[0];
	$isnext = $numrows-$pagesize*$pagenum;
	if($isnext>0){$isnext=1;}else{$isnext=0;}
	$sqlpage = ($pagesize-1)*$pagenum;	
	
	$r = $member_db->query("SELECT * FROM v9_invite where fcuser=".$memberinfo['userid']." OR users LIKE '%".$memberinfo['userid']."%' order by fctime DESC LIMIT ".$sqlpage.",".$pagenum);
	$r = $member_db->fetch_array($r);
	$member_db->set_model(10);
	if($r){
		foreach ($r AS $i => $row){
			$userinfo = $member_db->get_one(array('userid'=>$row['fcuser']));
			 
			$output['list'][$i]['id'] = $row['id'];
			$output['list'][$i]['name'] =  $userinfo['name'];
			$output['list'][$i]['image'] =  $userinfo['image'];
			
			if($memberinfo['userid'] == $row['fcuser']){
				$output['list'][$i]['myself'] = 1;
			}else{
				$output['list'][$i]['myself'] = 0;
			}
			$output['list'][$i]['message'] = $row['message'];
			$output['list'][$i]['num'] = $row['num'];
			$output['list'][$i]['time'] = $row['fctime'];
			
			$orderinfo = $order_db->get_one(array('order_id'=>$row['orderid']));
			$orderinfo['goods'] =  string2array($orderinfo['goods']);
			$output['list'][$i]['orderinfo'] = $orderinfo['goods'][$orderinfo['frist_good']]['title'];
			$output['list'][$i]['timshop'] = $orderinfo['timeshop'];
			
			if($orderinfo['timeshop']<SYS_TIME){ $timeout = 1; }else{ $timeout = 0;  }
			$output['list'][$i]['timeout'] = $timeout;
			
			$userarr = string2array($row['users']);
			$output['list'][$i]['users'] = $userarr;
		}
	}else{
			$output['list'] ="";
	}
	$output['next'] = $isnext;
	$output['page'] = $pagesize;
	outget(JSON($output));
}

function invite_add_goto(){
	$invite_db = pc_base::load_model('invite_model');
	$order_db = pc_base::load_model('order_model');
	$member_db = pc_base::load_model('member_model');
	$memberinfo = islogin();
	
	$invite_id  = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');
	$users   = isset($_REQUEST['users']) ? $_REQUEST['users'] : exit('error! No users .');
	
	$is_invited = $invite_db->get_one(array('id'=>$invite_id));
	if($is_invited){
		//判断订单是否正常
		$orderinfo = $order_db->get_one(array('order_id'=>$is_invited['orderid']));
		if($orderinfo){
			if($orderinfo['timeshop']<SYS_TIME){
				$output['status']='0';
				$output['info']='22';
				outget(JSON($output));
			}else{
			
				$userarr_old = string2array($is_invited['users']);
				$is_change = 0;
				
				$userlist = explode(",", $users);
				if($userlist){
					foreach ($userlist as $i => $s){
						$userdata = $member_db->query("SELECT * FROM v9_member_detail WHERE userid=".$s); 
						$userdata  =mysql_fetch_assoc($userdata);
						if($userdata){
							foreach ($userarr_old as $k => $value){
								if ($s==$value['userid']){
									break;
								}else{
									$userarr[$i]['userid'] = $s;
									$userarr[$i]['name'] = $userdata['name'];
									$userarr[$i]['image'] = $userdata['image'];
									$userarr[$i]['status'] = 0;
								}
							}
						}else{
							$output['status']='0';
							$output['info']='16';
							outget(JSON($output));
						}
					}
				}else{
					$output['status']='0';
					$output['info']='16';
					outget(JSON($output));
				}
					
				if($userarr){
					$userarray = array_merge($userarr,$userarr_old);
					$invite_db->update(array('users'=>array2string($userarray)),array('id'=>$invite_id));
				}
				$output['status']='1';
				$output['info']='';
				outget(JSON($output));
			}
			
			
		}else{
			$output['status']='0';
			$output['info']='19';
		}
	}else{
		$output['status']='0';
		$output['info']='19';
	}
	outget(JSON($output));
}


function invite_agree(){
	$invite_db = pc_base::load_model('invite_model');
	$order_db = pc_base::load_model('order_model');
	$member_db = pc_base::load_model('member_model');
	$memberinfo = islogin();
	
	$invite_id  = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');
	
	$is_invited = $invite_db->get_one(array('id'=>$invite_id));
	if($is_invited){
		//判断订单是否正常
		$orderinfo = $order_db->get_one(array('order_id'=>$is_invited['orderid']));
		if($orderinfo){
			if($orderinfo['timeshop']<SYS_TIME){
				$output['status']='0';
				$output['info']='22';
				outget(JSON($output));
			}else{
			
				$userarr = string2array($is_invited['users']);
				$is_change = 0;
				foreach($userarr as $i => $val){
					if($val['userid']==$memberinfo['userid']){
						$userarr[$i]['status'] = 1;
						$is_change = 1;
					}
				}
				if($is_change==1){
					$invite_db->update(array('users'=>array2string($userarr)),array('id'=>$invite_id));
					$output['status']='1';
					$output['info']='';
					outget(JSON($output));
				}else{
					$output['status']='0';
					$output['info']='23';
					outget(JSON($output));
				}
			}
			
		}else{
			$output['status']='0';
			$output['info']='19';
		}
	}else{
		$output['status']='0';
		$output['info']='19';
	}
	outget(JSON($output));
}


//登录
function login() {
	
	$username = isset($_REQUEST['username']) ? safe_replace($_REQUEST['username']) : exit('error! No username .');
	$password = isset($_REQUEST['password']) ? $_REQUEST['password'] : exit('error! No password.');
	
	$member_db = pc_base::load_model('member_model');
	$output = array();
	_session_start();
	$http_user_agent =  isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'appbaba';
	$password = decrypt('fuck_369', $password);  //DES解密

	//查询帐号
	$r = $member_db->get_one(array('username'=>$username));
	if(!$r){
		$output['status']='0';
		$output['info']='2';//用户不存在
	}else{
		$password = md5(md5(trim($password)).$r['encrypt']);
		//验证用户密码
		if($r['password'] != $password) {				
			$output['status']='0';
			$output['info']='3'; //密码错误
		}
		//用户锁定
		elseif($r['islock']){
			$output['status']='0';
			$output['info']='4';//用户被锁定
		}else{
			//登录成功 插入会员模型数据
			$otherinfo = array();
			$otherinfo['systype'] = isset($_REQUEST['systype']) ? safe_replace($_REQUEST['systype']) : exit('error! No systype.');
			$otherinfo['token'] = isset($_REQUEST['token']) ? safe_replace($_REQUEST['token']) : exit('error! No token.');
			if($otherinfo){
				require_once CACHE_MODEL_PATH.'member_input.class.php';
				require_once CACHE_MODEL_PATH.'member_update.class.php';
				$member_input = new member_input($r['modelid']);
				$user_model_info = $member_input->get($otherinfo);
				$member_db->set_model($r['modelid']);
				$member_db->update($user_model_info,array('userid'=>$r['userid']));
			}

			$phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key').$http_user_agent);
			$phpcms_auth = sys_auth($r['userid']."\t".$password, 'ENCODE', $phpcms_auth_key);
			param::set_cookie('auth', $phpcms_auth);
			$output['status']='1';
			$output['info']='5'; //登录成功
		}
	}
	outget(JSON($output));
}

//注册
function reg_code() {
	
	$phone = isset($_REQUEST['phone']) ? safe_replace($_REQUEST['phone']) : exit('error! No phone .');
	
	$member_db = pc_base::load_model('member_model');
	$output = array();
	_session_start();

	//查询帐号
	$r = $member_db->get_one(array('username'=>$phone));
	if(!$r){
		$userinfo = array();
		$userinfo['username'] = $phone;
		$userinfo['mobile'] = $phone;
		//$userinfo['code'] = mt_rand(1000,9999);
		$userinfo['code'] = 1234;
		$userinfo['email'] = $userinfo['username'].'@appbaba.com';
		$userinfo['password'] = '123456';
		$userinfo['encrypt'] = create_randomstr(6);
		$userinfo['modelid'] = 10;
		$userinfo['groupid'] = 2;
		$userinfo['regip'] = ip();
		$userinfo['islock'] = 1;
		$userinfo['point'] = $member_setting['defualtpoint'] ? $member_setting['defualtpoint'] : 0;
		$userinfo['amount'] = $member_setting['defualtamount'] ? $member_setting['defualtamount'] : 0;
		$userinfo['regdate'] = $userinfo['lastdate'] = SYS_TIME;
		$userinfo['siteid'] = 1;
		
		$userinfo['password'] = password($userinfo['password'], $userinfo['encrypt']);
		$userid = $member_db->insert($userinfo, 1);
		
		$output['status']='1';
		$output['info']='6';//注册发送成功
		
		sendsms($phone,$userinfo['code']);
	}else{
		if($r['islock']==0){
			$output['status']='0';
			$output['info']='7';//号码已经存在
		}else{
			$output['status']='1';
			$output['info']='8';//已存在,成功可继续操作
		}
	}
	
	outget(JSON($output));
}

function reg_verify() {
	
	$phone = isset($_REQUEST['phone']) ? safe_replace($_REQUEST['phone']) : exit('error! No phone .');
	$code = isset($_REQUEST['code']) ? intval($_REQUEST['code']) : exit('error! No code .');
	$http_user_agent =  isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'appbaba';
	
	$member_db = pc_base::load_model('member_model');
	$output = array();
	_session_start();


	//查询帐号
	$r = $member_db->get_one(array('username'=>$phone));
	if(!$r){
		$output['status']='0';
		$output['info']='9';//系统出错,号码已不存在
	}else{
		if($r['code']==$code && $r['islock']==1){
			$output['status']='1';
			$output['info']='10';//注册认证成功
			
			$member_db->update(array('islock'=>0,'groupid'=>2),array('userid'=>$r['userid']));
			//通过模型获取会员信息					
			require_once CACHE_MODEL_PATH.'member_input.class.php';
			require_once CACHE_MODEL_PATH.'member_update.class.php';
			$member_input = new member_input($r['modelid']);
			$user_model_info = $member_input->get($otherinfo);
			$user_model_info['userid'] = $r['userid'];

			//插入会员模型数据
			$member_db->set_model($r['modelid']);
			$member_db->insert($user_model_info);

			$phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key').$http_user_agent);
			$phpcms_auth = sys_auth($r['userid']."\t".$r['password'], 'ENCODE', $phpcms_auth_key);
			param::set_cookie('auth', $phpcms_auth);
		}else{
			$output['status']='0';
			$output['info']='11';//认证失败或已认证
		}
	}
	outget(JSON($output));
}

function reg_done() {
	$member_db = pc_base::load_model('member_model');
	$http_user_agent =  isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'appbaba';
		
	$memberinfo = islogin();
		
	$updateinfo = array();
	$updateinfo['password'] = isset($_REQUEST['password']) ? $_REQUEST['password'] : exit('error! No password .');
	$updateinfo['password'] = decrypt('fuck_369', $updateinfo['password']);  //DES解密 */
	$updateinfo['password'] = md5(md5(trim($updateinfo['password'])).$memberinfo['encrypt']);
	
	
	$password = md5(md5(trim('123456')).$memberinfo['encrypt']);
	//验证用户密码
	if($memberinfo['password'] != $password) {				
		$output['status']='0';
		$output['info']='13';//原密码错误
	}else{
		$otherinfo = array();
		$otherinfo['name'] = isset($_REQUEST['nickname']) ? safe_replace($_REQUEST['nickname']) : exit('error! No nickname .');
		$otherinfo['sex'] = isset($_REQUEST['sex']) ? safe_replace($_REQUEST['sex']) : exit('error! No sex .');
		$otherinfo['systype'] = isset($_REQUEST['systype']) ? safe_replace($_REQUEST['systype']) : exit('error! No systype .');
		$otherinfo['token'] = isset($_REQUEST['token']) ? safe_replace($_REQUEST['token']) : exit('error! No token .');
		
		$member_db->update($updateinfo, array('userid'=>$memberinfo['userid']));
		
		if($otherinfo){
			require_once CACHE_MODEL_PATH.'member_input.class.php';
			require_once CACHE_MODEL_PATH.'member_update.class.php';
			$member_input = new member_input(10);
			$user_model_info = $member_input->get($otherinfo);

			//插入会员模型数据
			$member_db->set_model(10);
			$member_db->update($user_model_info,array('userid'=>$memberinfo['userid']));
		}
		$output['status']='1';
		$output['info']='14';//注册成功
		//更新密码,使用新的cookie
		$phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key').$http_user_agent);
		$phpcms_auth = sys_auth($memberinfo['userid']."\t".$updateinfo['password'] , 'ENCODE', $phpcms_auth_key);
		param::set_cookie('auth', $phpcms_auth);
		
	}

	outget(JSON($output));
}

//忘记密码
function forget_code() {
	
	$phone = isset($_REQUEST['phone']) ? safe_replace($_REQUEST['phone']) : exit('error! No phone .');
	
	$member_db = pc_base::load_model('member_model');
	$output = array();
	_session_start();

	//查询帐号
	$r = $member_db->get_one(array('username'=>$phone));
	if(!$r){
		$output['status']='0';
		$output['info']='2';//号码不存在
	}else{
		$userinfo = array();
		//$userinfo['code'] = mt_rand(1000,9999);
		$userinfo['code'] = '1234';
		$userinfo['islock'] = 1;
		$member_db->update($userinfo, array('userid'=>$r['userid']));
		
		$output['status']='1';
		$output['info']='6';//修改密码查询号码成功
		
		sendsms($phone,$userinfo['code']);
	}
	
	
	outget(JSON($output));
}

function forget_verify() {
	
	$phone = isset($_REQUEST['phone']) ? safe_replace($_REQUEST['phone']) : exit('error! No phone .');
	$code = isset($_REQUEST['code']) ? intval($_REQUEST['code']) : exit('error! No code .');
	$http_user_agent =  isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'appbaba';
	
	$member_db = pc_base::load_model('member_model');
	$output = array();
	_session_start();


	//查询帐号
	$r = $member_db->get_one(array('username'=>$phone));
	if(!$r){
		$output['status']='0';
		$output['info']='9';//系统出错,号码已不存在
	}else{
		if($r['code']==$code && $r['islock']==1){
			$output['status']='1';
			$output['info']='10'; //认证成功
			
			$member_db->update(array('islock'=>0),array('userid'=>$r['userid']));
			$phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key').$http_user_agent);
			$phpcms_auth = sys_auth($r['userid']."\t".$r['password'], 'ENCODE', $phpcms_auth_key);
			param::set_cookie('auth', $phpcms_auth);
		}else{
			$output['status']='0';
			$output['info']='11'; //认证失败
		}
	}
	outget(JSON($output));
}

function forget_done() {
	$member_db = pc_base::load_model('member_model');
	$http_user_agent =  isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'appbaba';
	
	$memberinfo = islogin();
		
	$updateinfo = array();
	$updateinfo['password'] = isset($_REQUEST['password']) ? $_REQUEST['password'] : exit('error! No password .');
	
	$updateinfo['password'] = decrypt('fuck_369', $updateinfo['password']);  //DES解密 
	$updateinfo['password'] = md5(md5(trim($updateinfo['password'])).$memberinfo['encrypt']);

	//验证用户密码
	$otherinfo = array();
	$otherinfo['systype'] = isset($_REQUEST['systype']) ? safe_replace($_REQUEST['systype']) : exit('error! No systype .');
	$otherinfo['token'] = isset($_REQUEST['token']) ? safe_replace($_REQUEST['token']) : exit('error! No token .');
	
	$member_db->update($updateinfo, array('userid'=>$memberinfo['userid']));
	
	if($otherinfo){
		require_once CACHE_MODEL_PATH.'member_input.class.php';
		require_once CACHE_MODEL_PATH.'member_update.class.php';
		$member_input = new member_input(10);
		$user_model_info = $member_input->get($otherinfo);

		//插入会员模型数据
		$member_db->set_model(10);
		$member_db->update($user_model_info,array('userid'=>$memberinfo['userid']));
	}
	$output['status']='1';
	$output['info']='17';//修改密码成功
	//更新密码,使用新的cookie
	$phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key').$http_user_agent);
	$phpcms_auth = sys_auth($memberinfo['userid']."\t".$updateinfo['password'] , 'ENCODE', $phpcms_auth_key);
	param::set_cookie('auth', $phpcms_auth);
		
	outget(JSON($output));
}


function member_center() {
		$member_db = pc_base::load_model('member_model');
		
		$memberinfo = islogin();

		$output['nickname'] = $memberinfo['name'];
		$output['pic'] =  $memberinfo['image'];
		$output['groupid'] = $memberinfo['vip'];
		$output['phone'] = $memberinfo['mobile'];
		$output['overduedate'] = $memberinfo['overduedate'];
		$output['cardid'] = $memberinfo['userid']+100000000;
		$output['point'] = $memberinfo['point'];
		$output['sex'] = $memberinfo['sex'];
		$output['userid'] = $memberinfo['userid'];
		$output['ding_tui'] = 1;//退订
		$output['ding_done'] = 1;//完订
		$output['ding_pass'] = 1;//逃订
		$output['url_point'] = "http://www.baidu.com";//积分规则
		$output['url_about'] = "http://www.baidu.com";//关于我们

		outget(JSON($output));
}

function member_photo() {
		$member_db = pc_base::load_model('member_model');
		$memberinfo = islogin();
		
		$file = $_FILES['image']['name'];
		require_once 'UploadFile.php';
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  './uploadfile/av/';// 设置附件上传目录
		$upload->saveRule =  $memberinfo['userid'];//重命名
		if(!$upload->upload()) {// 上传错误提示错误信息
			$output['status'] = "0";
			$output['pic'] = "";
			$output['info'] = $upload->getErrorMsg();
		}else{
			$info = $upload->getUploadFileInfo();
			$content = APP_PATH.'uploadfile/av/'.$info[0]['savename'];
			$output['status'] = "1";
			$output['pic'] = $content;
			$output['info'] = "成功";
			
			$userinfo['image'] = $content;
			$member_db->set_model(10);
			$member_db->update($userinfo, array('userid'=>$memberinfo['userid']));
		}

		outget(JSON($output));
}


function member_edit() {
	$member_db = pc_base::load_model('member_model');
		
	$memberinfo = islogin();
	$otherinfo = array();
	
	//是否有密码
	if($_REQUEST['old_password']){
		$old_password  = isset($_REQUEST['old_password']) ? $_REQUEST['old_password'] : exit('error! No old_password .');
		$old_password = decrypt('fuck_369', $old_password);  //DES解密 
		$old_password = md5(md5(trim($old_password)).$memberinfo['encrypt']);
		
		if($memberinfo['password'] != $old_password) {				
			$output['status']='0';
			$output['info']='13';//原密码错误
		}else{
			if($_REQUEST['password']){
				$updateinfo['password']  = isset($_REQUEST['password']) ? $_REQUEST['password'] : exit('error! No password .');
				$updateinfo['password'] = decrypt('fuck_369', $updateinfo['password']);  //DES解密 
				$updateinfo['password']  = md5(md5(trim($updateinfo['password'])).$memberinfo['encrypt']);
			}
		}
	}
	//是否有昵称
	if($_REQUEST['nickname']){
		$otherinfo['name']  = isset($_REQUEST['nickname']) ? safe_replace($_REQUEST['nickname']) : exit('error! No nickname .');
	}
	
	//是否有性别
	if($_REQUEST['sex']){
		$otherinfo['sex']  = isset($_REQUEST['sex']) ? safe_replace($_REQUEST['sex']) : exit('error! No sex .');
	}

	
	if($updateinfo){
		$member_db->update($updateinfo, array('userid'=>$memberinfo['userid']));
	}
	if($otherinfo){
		require_once CACHE_MODEL_PATH.'member_input.class.php';
		require_once CACHE_MODEL_PATH.'member_update.class.php';
		$member_input = new member_input(10);
		$user_model_info = $member_input->get($otherinfo);

		//插入会员模型数据
		$member_db->set_model(10);
		$member_db->update($user_model_info,array('userid'=>$memberinfo['userid']));
	}

	$output['status']='1';
	$output['info']='操作成功';//原密码错误
	
	//更新密码,使用新的cookie
	$phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key').$http_user_agent);
	$phpcms_auth = sys_auth($memberinfo['userid']."\t".$updateinfo['password'] , 'ENCODE', $phpcms_auth_key);
	param::set_cookie('auth', $phpcms_auth);
			
	outget(JSON($output));
	
}

function update() {
	$output['upVName'] = "0.1.1";
	$output['upVCode'] = "1";
	$output['upUrl'] = "http://www.appbaba.com/api/apk.apk";
	$output['upSize'] = "10M";
	$output['updateContent'] = "123123123123";

	outget(JSON($output));
}

//im
function im_search() {
	$member_db = pc_base::load_model('member_model');
		
	//$memberinfo = islogin();
	$key  = isset($_REQUEST['key']) ? safe_replace($_REQUEST['key']) : exit('error! No key .');
	
	$pagenum = 20;
	if($_REQUEST['page']){ $pagesize = intval($_REQUEST['page']); }else{$pagesize =1;}
	$countrs = $member_db->query("SELECT count(*) FROM phpcms_member a, phpcms_member_detail b where a.userid=b.userid AND (a.username LIKE '%".$key."%' or b.name LIKE '%".$key."%') AND a.modelid=10");   //取数量,判断是否有分页
	$myrow = mysql_fetch_array($countrs);
	$numrows=$myrow[0];
	$isnext = $numrows-$pagesize*$pagenum;
	if($isnext>0){$isnext=1;}else{$isnext=0;}
	$sqlpage = ($pagesize-1)*$pagenum;	
	
	$r = $member_db->query("SELECT * FROM phpcms_member a, phpcms_member_detail b where a.userid=b.userid AND (a.username LIKE '%".$key."%' or b.name LIKE '%".$key."%') AND a.modelid=10 LIMIT ".$sqlpage.",".$pagenum);
	$r = $member_db->fetch_array($data);
	foreach ($r AS $i => $row){ 
		$output['list'][$i]['id'] = $row['userid'];
		$output['list'][$i]['name'] =  $row['name'];
		$output['list'][$i]['photo'] = $row['image'];
	}
	$output['next'] = $isnext;
	$output['page'] = $pagesize;
	outget(JSON($output));

}

function im_addfriend() {
	$member_db = pc_base::load_model('member_model');
	$friend_db = pc_base::load_model('friend_model');
		
	$memberinfo = islogin();
	$friend_id  = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');
	
	$r = $member_db->get_one(array('userid'=>$friend_id));
	if(!$r){
		$output['status']='0';
		$output['info']='16';//添加好友失败,错误ID
	}else{
		$isadd = $friend_db->get_one(array('userid'=>$memberinfo['userid'],'friendid'=>$friend_id));
		if($isadd){
			if($isadd['iss']==1){
				$output['status'] = "0";
				$output['info'] = "17";//已经是好友了
			}else{
				$friend_db->update(array('adddate'=>SYS_TIME),array('id'=>$isadd['id']));
				$output['status'] = "1";
				$output['info'] = "添加好友成功,等待对方验证";
			}
		}else{
			$data = array('userid'=>$memberinfo['userid'], 'friendid'=>$friend_id, 'nickname'=>$memberinfo['name'], 'adddate'=>SYS_TIME);
			$friend_db->insert($data);
			$output['status'] = "1";
			$output['info'] = "添加好友成功,等待对方验证";
		}
	}
	outget(JSON($output));

}

function im_newfriend() {
	$member_db = pc_base::load_model('member_model');
	$friend_db = pc_base::load_model('friend_model');
		
	$memberinfo = islogin();

	$pagenum = 20;
	if($_REQUEST['page']){ $pagesize = intval($_REQUEST['page']); }else{$pagesize =1;}
	$countrs = $friend_db->query("SELECT count(*) FROM phpcms_friend where iss=0 and friendid=".$memberinfo['userid']);   //取数量,判断是否有分页
	$myrow = mysql_fetch_array($countrs);
	$numrows=$myrow[0];
	$isnext = $numrows-$pagesize*$pagenum;
	if($isnext>0){$isnext=1;}else{$isnext=0;}
	$sqlpage = ($pagesize-1)*$pagenum;	
	
	$r = $member_db->query("SELECT * FROM phpcms_friend a,phpcms_member b,phpcms_member_detail c where a.userid=b.userid AND b.userid=c.userid and a.friendid=".$memberinfo['userid']." and a.iss=0 order by a.adddate DESC LIMIT ".$sqlpage.",".$pagenum);
	$r = $member_db->fetch_array($data);
	foreach ($r AS $i => $row){ 
		$output['list'][$i]['id'] = $row['userid'];
		$output['list'][$i]['name'] =  $row['name'];
		$output['list'][$i]['photo'] = $row['image'];
	}
	$output['next'] = $isnext;
	$output['page'] = $pagesize;
	outget(JSON($output));
}

function im_newfriend_verify() {
	$member_db = pc_base::load_model('member_model');
	$friend_db = pc_base::load_model('friend_model');
		
	$memberinfo = islogin();
	$friend_id  = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');
	
	$r = $friend_db->get_one(array('userid'=>$friend_id,'friendid'=>$memberinfo['userid'],'iss'=>0));
	if(!$r){
		$output['status']='0';
		$output['info']='16';//添加好友失败,错误ID
	}else{
		$data = array('iss'=>1);
		$friend_db->update($data,array('userid'=>$friend_id,'friendid'=>$memberinfo['userid']));
 		$data2 = array('userid'=>$memberinfo['userid'], 'friendid'=>$friend_id, 'nickname'=>$r['nickname'], 'iss'=>1, 'adddate'=>SYS_TIME);
		$friend_db->insert($data2); 
		$output['status'] = "1";
		$output['info'] = "添加好友成功";
	}
	outget(JSON($output));
}

function im_list() {
	$member_db = pc_base::load_model('member_model');
	$friend_db = pc_base::load_model('friend_model');
		
	$memberinfo = islogin();

	$r = $member_db->query("SELECT * FROM phpcms_friend a,phpcms_member b,phpcms_member_detail c where a.friendid=b.userid and b.userid=c.userid and a.userid=".$memberinfo['userid']." and a.iss=1 order by a.adddate DESC LIMIT 200");
	$r = $member_db->fetch_array($data);
	foreach ($r AS $i => $row){ 
		$output[$i]['id'] = $row['friendid'];
		$output[$i]['name'] =  $row['name'];
		$output[$i]['photo'] = $row['image'];
	}
	outget(JSON($output));
}

function im_removefriend() {
	$friend_db = pc_base::load_model('friend_model');
	$member_db = pc_base::load_model('member_model');
	$memberinfo = islogin();
	$friend_id  = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : exit('error! No id .');

	$friend_db->delete(array('userid'=>$memberinfo['userid'], 'friendid'=>$friend_id));
	$friend_db->delete(array('userid'=>$friend_id, 'friendid'=>$memberinfo['userid']));
	$output['status'] = "1";
	$output['info'] = "删除好友成功";
	
	outget(JSON($output));
}

function im_chat() {
	$message_db = pc_base::load_model('message_model');
	$memberinfo = islogin();
	$receive_id  = isset($_REQUEST['receive_id']) ? intval($_REQUEST['receive_id']) : exit('error! No receive_id .');
	$content  = isset($_REQUEST['content']) ? $_REQUEST['content'] : exit('error! No content .');
	
	$r = $message_db->query("SELECT * FROM v9_member_detail WHERE userid=".$receive_id);   //app资料
	$r  =mysql_fetch_assoc($r);
	if(!$r){
		$output['status']='0';
		$output['info']='2';//用户不存在
	}else{
		$data2 = array('send_id'=>$memberinfo['userid'], 'receive_id'=>$receive_id, 'type'=>'text', 'content'=>$content, 'time'=>SYS_TIME);
		$message_db->insert($data2); 
		
		if($r['systype']=="android"){
			require_once 'SAM/php_sam.php';
			$conn = new SAMConnection();
			$conn->connect(SAM_MQTT, array(SAM_HOST => 'www.appbaba.com', SAM_PORT => 1883));  
			$androidcon = '{"send_id":"'.$memberinfo['userid'].'","receive_id":"'.$receive_id.'","time":"'.SYS_TIME.'","content":"'.$content.'"}';                        
			$msgCpu = new SAMMessage($androidcon);
			$conn->send('topic://998/'.$r['token'], $msgCpu);
            $conn->disconnect(); 
		}elseif($r['systype']=="ios"){
			$body['aps'] = array( 'alert' => $content,  'sound' => 'default' ); 
			$body['action'] = array( 'send_id' => $memberinfo['userid'],  'receive_id' => $receive_id, 'content' => $content, 'time' =>SYS_TIME); 
			$ctx = stream_context_create();
			$pem = 'E:\369\api\SAM\369.pem';
			stream_context_set_option($ctx,"ssl","local_cert",$pem);
			$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
			if (!$fp) {
				$output['status'] = 'error';
			}else{
				$payload = json_encode($body); 
				
				$msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $userinfo[0]['token'])) . pack("n",strlen($payload)) . $payload;
				$result = fwrite($fp, $msg, strlen($msg));
				if (!$result){
					$output['status'] = 'error';
				}
			}
		}
		
		$output['status'] = "1";
		$output['time'] = SYS_TIME;
	}
	
	outget(JSON($output));
}




//通用函数
function arrayRecursive(&$array, $function, $apply_to_keys_also = false){
    static $recursive_counter = 0;
    if (++$recursive_counter > 1000) {
        die('possible deep recursion attack');
    }
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $function, $apply_to_keys_also);
        } else {
            $array[$key] = $function($value);
        }

        if ($apply_to_keys_also && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
    $recursive_counter--;
}
function multi_array_sort($multi_array,$sort_key,$sort=SORT_ASC){ 
	if(is_array($multi_array)){ 
		foreach ($multi_array as $row_array){ 
			if(is_array($row_array)){ 
				$key_array[] = $row_array[$sort_key]; 
			}else{ 
				return false; 
			} 
		} 
	}else{ 
		return false; 
	} 
	array_multisort($key_array,$sort,$multi_array); 
	return $multi_array; 
} 



function JSON($array) {
	$action = safe_replace($_REQUEST['action']);
	//print_R($action);
	$barr[$action]=$array;
    arrayRecursive($barr, 'urlencode', true);
    $json = json_encode($barr);
    return urldecode($json);
}
//重命名数组  array_walk($r, 'foo', $kname);
function foo(&$v, $k, $kname) {
  $v = array_combine($kname,$v);
}

function outget($getcon){
		$hash = md5($getcon);
		header("ETag: \"$hash\"");
		echo $getcon;
		exit();
}



/**
 * 初始化phpsso
 * about phpsso, include client and client configure
 * @return string phpsso_api_url phpsso地址
 */


function _checkname($username) {
	$username =  trim($username);
	if ($this->db->get_one(array('username'=>$username))){
		return false;
	}
	return true;
}

function _session_start() {
	$session_storage = 'session_'.pc_base::load_config('system','session_storage');
	pc_base::load_sys_class($session_storage);
}

function decrypt ($key, $decrypt){
    // 不需要設定 IV
    $str = mcrypt_decrypt(MCRYPT_DES, $key, base64_decode($decrypt), MCRYPT_MODE_ECB);

    // 根據 PKCS#7 RFC 5652 Cryptographic Message Syntax (CMS) 修正 Message 移除 Padding
    $pad = ord($str[strlen($str) - 1]);
    return substr($str, 0, strlen($str) - $pad);
}

function encrypt ($key, $encrypt){
    // 根據 PKCS#7 RFC 5652 Cryptographic Message Syntax (CMS) 修正 Message 加入 Padding
    $block = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_ECB);
    $pad = $block - (strlen($encrypt) % $block);
    $encrypt .= str_repeat(chr($pad), $pad);

    // 不需要設定 IV 進行加密
    $passcrypt = mcrypt_encrypt(MCRYPT_DES, $key, $encrypt, MCRYPT_MODE_ECB);
    return base64_encode($passcrypt);
}

function thepos($catid, $symbol=' > '){
	$appid = $_REQUEST['appid'];
	$category_arr = array();
	$category_arr = getcache('member_'.$appid.'_category_content_1','commons');
	if(!isset($category_arr[$catid])) return '';
	$pos = '';
	$siteurl = siteurl($category_arr[$catid]['siteid']);
	$arrparentid = array_filter(explode(',', $category_arr[$catid]['arrparentid'].','.$catid));
	foreach($arrparentid as $catid) {
		$pos .= $category_arr[$catid]['catname'].$symbol;
	}
	return $pos;
}






//取广告
function getad($posid,$num){
	if(!$num) $num = 2;
	$catcache = getcache('category_content_1','commons');
	$posterdb = pc_base::load_model('poster_model');
	$ad = $posterdb->query("SELECT * FROM phpcms_position_data WHERE posid=".$posid." ORDER BY listorder DESC LIMIT ".$num);
	$adr = $posterdb->fetch_array($ad);
	foreach ($adr AS $i => $row){ 
		$rowdata = string2array($row['data']);
		$addata[$i]['id'] = $row['id'];
		$addata[$i]['type'] = $catcache[$row['catid']]['description'];
		$addata[$i]['thumb'] = $rowdata['adurl'];
	}
	return $addata;
}


function islogin(){
	$member_db = pc_base::load_model('member_model');
	$http_user_agent =  isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'appbaba';

	$phpcms_auth = param::get_cookie('auth');
	if ($phpcms_auth) {
		$auth_key = $auth_key = md5(pc_base::load_config('system', 'auth_key').$http_user_agent);
		list($userid, $password) = explode("\t", sys_auth($phpcms_auth, 'DECODE', $auth_key));
		
		$memberinfo = $member_db->get_one(array('userid'=>$userid));
		if(!$memberinfo){
		
			$output['status']='0';
			$output['info']='12';//cookie错误
			outget(JSON($output));
			exit;
			
		}else{
			//获取用户模型信息
			$member_db->set_model($memberinfo['modelid']);
			$_member_modelinfo = $member_db->get_one(array('userid'=>$userid)); 
			$_member_modelinfo = $_member_modelinfo ? $_member_modelinfo : array();
			$member_db->set_model();
			if(is_array($memberinfo)) {
				$memberinfo = array_merge($memberinfo, $_member_modelinfo);
			}
			
			if($memberinfo && $memberinfo['password'] === $password) {
				$islogin = 1;
			} else {
				param::set_cookie('auth', '');
				$islogin = 0;
			}
		}
		unset($userid, $password, $phpcms_auth, $auth_key);
	} else {
		$islogin = 0;
	}
	
	if($islogin==1){
		return $memberinfo;
	}else{
		$output['status']='0';
		$output['info']='1';//无cookie请先登录
	
		outget(JSON($output));
		exit;
	}
	
}


function sendsms($phone,$str){
		//$result = file_get_contents('http://sms.mobset.com/SDK/Sms_Send.asp?CorpID=113635&LoginName=jxt&Passwd=792949&send_no='.$phone.'&msg='.iconv('UTF-8', 'GB2312', $str));
/* 		$resultarr =explode(',',$result);
		if ($resultarr[0] ==1){
			return 1;
		}else{
			return $result;
		} */
}


?>